package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }

    @Override
    @Transactional
    public void addFlavor(DishDTO dishDTO) {

        Dish dish = new Dish();

        BeanUtils.copyProperties(dishDTO, dish);

        dishMapper.insert(dish);

        Long dishId = dish.getId();

        List<DishFlavor> flavors = dishDTO.getFlavors();

        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> {
                dishFlavor.setDishId(dishId);
            });
            //向口味表插入n条数据
            dishFlavorMapper.insertBatch(flavors);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page =(Page<DishVO>) dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 思路：
     * 1. 检查菜品是否正在使用，否则不能删除
     * 2. 检查套餐是否关联了菜品，否则不能删除
     * 3. 先删除菜品，然后删除口味
     * @param ids
     */
    @Transactional
    @Override
    public void deleteDishByIds(List<Long> ids) {
        // 检查菜品是否正在使用，否则不能删除
        for(Long id :ids){
            // 根据ID，一个一个去检查，如果发现正在使用，就抛出业务异常
            Dish dish = dishMapper.selectById(id);
            if(dish.getStatus() == StatusConstant.ENABLE){
                // 抛出异常
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        // 检查套餐是否关联了菜品，否则不能删除
        Long number = dishMapper.selectCountByDishIds(ids);
        if (number !=0 ){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        // 先删除菜品，然后删除口味
        for (Long id :ids){
            // 删除菜品
            dishMapper.deleteById(id);
            // 删除菜品对应口味
            dishFlavorMapper.deleteByDishId(id);
        }
    }

    @Override
    public DishVO findById(Long id) {
        // 1. 根据ID，获取菜品对象
        Dish dish = dishMapper.selectById(id);
        // 2. 根据ID，获取菜品口味列表
        List<DishFlavor> dishFlavorList = dishFlavorMapper.selectByDishId(id);
        // 3. 封装DishVO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);//把dish对象的数据复制到dishVO
        dishVO.setFlavors(dishFlavorList);
        return dishVO;
    }

    @Transactional
    @Override
    public void updateDishById(DishDTO dishDTO) {
        //更新菜品表
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.updateDish(dish);
        // 更新口味表
        // 把之前的口味删除
        dishFlavorMapper.deleteByDishId(dish.getId());
        // 再增加新的口味
        List<DishFlavor> flavors = dishDTO.getFlavors();
        // 这个列表，缺少了菜品ID，需要遍历列表，把菜品的ID赋值给口味对象
        for(DishFlavor dishFlavor:flavors){
            dishFlavor.setDishId(dish.getId());
        }
        // 调用mapper完成批量添加
        dishFlavorMapper.insertBatch(flavors);
    }

    @Override
    public List<Dish> selectCateGoryById(Long id) {
        Dish dish = Dish.builder()
                .categoryId(id)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.selectCategoryById(dish);
    }
}

